import { Body, Controller, Delete, Get, Options, Param, Post, Put, Query } from '@nestjs/common';
import { ApiOperation, ApiProperty, ApiPropertyOptional, ApiTags } from '@nestjs/swagger';
import { PostModel } from './posts.modle';
// import { get } from 'http';

class CreatPostDto {
    @ApiProperty({
        description: '帖子标题',
        example: '帖子标题1'
    })
    title: string

    @ApiProperty({
        description: '帖子内容',
        example: '帖子内容1'
    })
    content: string
}

@Controller('posts')
@ApiTags('帖子')
export class PostsController {
    @Get()
    @ApiOperation({ summary: '帖子列表' })
    async index() {
        return await PostModel.find()
    }

    @Post()
    @ApiOperation({ summary: '创建帖子' })
    async create(@Body() creatPostDto: CreatPostDto) {
        await PostModel.create(creatPostDto)
        return {
            success: true
        }
    }

    @Get(':id')
    @ApiOperation({ summary: '帖子详情' })
    async detal(@Param('id') id: string) {
        return await PostModel.findById(id)

    }

    @Put(':id')
    @ApiOperation({ summary: '编辑帖子' })
    async update(@Param('id') id: string, @Body() updatePostDto: CreatPostDto) {
        await PostModel.findByIdAndUpdate(id, updatePostDto)
        return {
            success: true
        }
    }

    @Delete(':id')
    @ApiOperation({ summary: '删除帖子' })
    remove(@Param('id') id: string) {
        return {
            success: true
        }
    }
}
